const fp = require('lodash/fp')
const cars = [{
  name:'Ferrari FF',
  hoursepower:660,
  dollor_value:700000,
  in_stock:true
},{
  name:'Spyker C12 Zagato',
  hoursepower:650,
  dollor_value:648000,
  in_stock:false
},{
  name:'Jaguar XKR-S',
  hoursepower:550,
  dollor_value:132000,
  in_stock:false
},{
  name:'Audi R8',
  hoursepower:625,
  dollor_value:114200,
  in_stock:false
},{
  name:'Aston Martin One-77',
  hoursepower:750,
  dollor_value:1850000,
  in_stock:true
},{
  name:'Pagani Huayra',
  hoursepower:700,
  dollor_value:1300000,
  in_stock:false
}]

//练习1:
let last_car = cars => fp.last(cars)
let in_stock_prop = last_car => fp.prop('in_stock',last_car)  //将两个步骤拆解为两个函数
let fn1 = fp.flowRight(in_stock_prop,last_car) //将两个函数组合成一个函数
console.log(fn1(cars))

//练习2
let first_car = cars => fp.first(cars)
let name_prop = car => fp.prop('name',car)
let fn2 = fp.flowRight(name_prop,first_car)
console.log(fn2(cars))

//练习3
let _average = xs => fp.reduce(fp.add,0,xs)/xs.length
let car_map = cars => fp.map((car)=>car.dollar_value,cars) //分离出遍历函数
let averageDollarValue = fp.flowRight(_average,car_map)
console.log(averageDollarValue(cars))

//练习4
const log = v => {
  console.log(v)
  return v
} 
let _underscore = fp.replace(/\W+/g,'_')
let _join = arr => arr.join('9999')
let split = str => str.split('9999')    //由于_underscore函数会将,' '等同样转换为'_'，所以加入以9999作为数组的分割，好让后面重新进行组合成为一个转换为小写以后的数组
let name_map = cars => fp.map(car=>car.name,cars) //遍历数组获取name
let sanitizeNames = fp.flowRight(split,fp.toLower,_underscore,_join,name_map)
console.log(sanitizeNames(cars))
